*****************************************************************************************
* 																						*
*Campaign Panel Book Chapter 2 - Intraindividual Dynamic of Reception Frequencies 		*		
*																						*	
*****************************************************************************************


*---------------------------------------------------------------
* Creating the main diagonal for successive wave pairs and combined for all waves
*---------------------------------------------------------------



foreach num1 of numlist 2/7  {
foreach num2 of numlist 2/7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_oeff`num1'  {		
		gen a_oeff`num1'`num2'_hd=.
		replace a_oeff`num1'`num2'_hd=1 if a_oeff`num1'==a_oeff`num2' & a_oeff`num1'<. & a_oeff`num2'<.
		replace a_oeff`num1'`num2'_hd=0 if a_oeff`num1'!=a_oeff`num2' & a_oeff`num1'<. & a_oeff`num2'<.
		lab var a_oeff`num1'`num2'_hd "Hauptdiagonale: Oeff 2009"
		tab a_oeff`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_oeff`num1'  {		
		gen b_oeff`num1'`num2'_hd=.
		replace b_oeff`num1'`num2'_hd=1 if b_oeff`num1'==b_oeff`num2' & b_oeff`num1'<. & b_oeff`num2'<.
		replace b_oeff`num1'`num2'_hd=0 if b_oeff`num1'!=b_oeff`num2' & b_oeff`num1'<. & b_oeff`num2'<.
		lab var b_oeff`num1'`num2'_hd "Hauptdiagonale: Oeff 2013"
		tab b_oeff`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_privat`num1'  {		
		gen a_privat`num1'`num2'_hd=.
		replace a_privat`num1'`num2'_hd=1 if a_privat`num1'==a_privat`num2' & a_privat`num1'<. & a_privat`num2'<.
		replace a_privat`num1'`num2'_hd=0 if a_privat`num1'!=a_privat`num2' & a_privat`num1'<. & a_privat`num2'<.
		lab var a_privat`num1'`num2'_hd "Hauptdiagonale: privat 2009"
		tab a_privat`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_privat`num1'  {		
		gen b_privat`num1'`num2'_hd=.
		replace b_privat`num1'`num2'_hd=1 if b_privat`num1'==b_privat`num2' & b_privat`num1'<. & b_privat`num2'<.
		replace b_privat`num1'`num2'_hd=0 if b_privat`num1'!=b_privat`num2' & b_privat`num1'<. & b_privat`num2'<.
		lab var b_privat`num1'`num2'_hd "Hauptdiagonale: privat 2013"
		tab b_privat`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_paper`num1'  {		
		gen a_paper`num1'`num2'_hd=.
		replace a_paper`num1'`num2'_hd=1 if a_paper`num1'==a_paper`num2' & a_paper`num1'<. & a_paper`num2'<.
		replace a_paper`num1'`num2'_hd=0 if a_paper`num1'!=a_paper`num2' & a_paper`num1'<. & a_paper`num2'<.
		lab var a_paper`num1'`num2'_hd "Hauptdiagonale: paper 2009"
		tab a_paper`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_paper`num1'  {		
		gen b_paper`num1'`num2'_hd=.
		replace b_paper`num1'`num2'_hd=1 if b_paper`num1'==b_paper`num2' & b_paper`num1'<. & b_paper`num2'<.
		replace b_paper`num1'`num2'_hd=0 if b_paper`num1'!=b_paper`num2' & b_paper`num1'<. & b_paper`num2'<.
		lab var b_paper`num1'`num2'_hd "Hauptdiagonale: paper 2013"
		tab b_paper`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_bild`num1'  {		
		gen a_bild`num1'`num2'_hd=.
		replace a_bild`num1'`num2'_hd=1 if a_bild`num1'==a_bild`num2' & a_bild`num1'<. & a_bild`num2'<.
		replace a_bild`num1'`num2'_hd=0 if a_bild`num1'!=a_bild`num2' & a_bild`num1'<. & a_bild`num2'<.
		lab var a_bild`num1'`num2'_hd "Hauptdiagonale: bild 2009"
		tab a_bild`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_bild`num1'  {		
		gen b_bild`num1'`num2'_hd=.
		replace b_bild`num1'`num2'_hd=1 if b_bild`num1'==b_bild`num2' & b_bild`num1'<. & b_bild`num2'<.
		replace b_bild`num1'`num2'_hd=0 if b_bild`num1'!=b_bild`num2' & b_bild`num1'<. & b_bild`num2'<.
		lab var b_bild`num1'`num2'_hd "Hauptdiagonale: bild 2013"
		tab b_bild`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_paper_and`num1'  {		
		gen a_paper_and`num1'`num2'_hd=.
		replace a_paper_and`num1'`num2'_hd=1 if a_paper_and`num1'==a_paper_and`num2' & a_paper_and`num1'<. & a_paper_and`num2'<.
		replace a_paper_and`num1'`num2'_hd=0 if a_paper_and`num1'!=a_paper_and`num2' & a_paper_and`num1'<. & a_paper_and`num2'<.
		lab var a_paper_and`num1'`num2'_hd "Hauptdiagonale: paper_and 2009"
		tab a_paper_and`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_paper_and`num1'  {		
		gen b_paper_and`num1'`num2'_hd=.
		replace b_paper_and`num1'`num2'_hd=1 if b_paper_and`num1'==b_paper_and`num2' & b_paper_and`num1'<. & b_paper_and`num2'<.
		replace b_paper_and`num1'`num2'_hd=0 if b_paper_and`num1'!=b_paper_and`num2' & b_paper_and`num1'<. & b_paper_and`num2'<.
		lab var b_paper_and`num1'`num2'_hd "Hauptdiagonale: paper_and 2013"
		tab b_paper_and`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_magazine`num1'  {		
		gen a_magazine`num1'`num2'_hd=.
		replace a_magazine`num1'`num2'_hd=1 if a_magazine`num1'==a_magazine`num2' & a_magazine`num1'<. & a_magazine`num2'<.
		replace a_magazine`num1'`num2'_hd=0 if a_magazine`num1'!=a_magazine`num2' & a_magazine`num1'<. & a_magazine`num2'<.
		lab var a_magazine`num1'`num2'_hd "Hauptdiagonale: magazine 2009"
		tab a_magazine`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_magazine`num1'  {		
		gen b_magazine`num1'`num2'_hd=.
		replace b_magazine`num1'`num2'_hd=1 if b_magazine`num1'==b_magazine`num2' & b_magazine`num1'<. & b_magazine`num2'<.
		replace b_magazine`num1'`num2'_hd=0 if b_magazine`num1'!=b_magazine`num2' & b_magazine`num1'<. & b_magazine`num2'<.
		lab var b_magazine`num1'`num2'_hd "Hauptdiagonale: magazine 2013"
		tab b_magazine`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_polinter`num1'  {		
		gen a_polinter`num1'`num2'_hd=.
		replace a_polinter`num1'`num2'_hd=1 if a_polinter`num1'==a_polinter`num2' & a_polinter`num1'<. & a_polinter`num2'<.
		replace a_polinter`num1'`num2'_hd=0 if a_polinter`num1'!=a_polinter`num2' & a_polinter`num1'<. & a_polinter`num2'<.
		lab var a_polinter`num1'`num2'_hd "Hauptdiagonale: polinter 2009"
		tab a_polinter`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_polinter`num1'  {		
		gen b_polinter`num1'`num2'_hd=.
		replace b_polinter`num1'`num2'_hd=1 if b_polinter`num1'==b_polinter`num2' & b_polinter`num1'<. & b_polinter`num2'<.
		replace b_polinter`num1'`num2'_hd=0 if b_polinter`num1'!=b_polinter`num2' & b_polinter`num1'<. & b_polinter`num2'<.
		lab var b_polinter`num1'`num2'_hd "Hauptdiagonale: polinter 2013"
		tab b_polinter`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_talk`num1'  {		
		gen a_talk`num1'`num2'_hd=.
		replace a_talk`num1'`num2'_hd=1 if a_talk`num1'==a_talk`num2' & a_talk`num1'<. & a_talk`num2'<.
		replace a_talk`num1'`num2'_hd=0 if a_talk`num1'!=a_talk`num2' & a_talk`num1'<. & a_talk`num2'<.
		lab var a_talk`num1'`num2'_hd "Hauptdiagonale: talk 2009"
		tab a_talk`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_talk`num1'  {		
		gen b_talk`num1'`num2'_hd=.
		replace b_talk`num1'`num2'_hd=1 if b_talk`num1'==b_talk`num2' & b_talk`num1'<. & b_talk`num2'<.
		replace b_talk`num1'`num2'_hd=0 if b_talk`num1'!=b_talk`num2' & b_talk`num1'<. & b_talk`num2'<.
		lab var b_talk`num1'`num2'_hd "Hauptdiagonale: talk 2013"
		tab b_talk`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}	

foreach num1 of numlist 2/6 {
foreach num2 of numlist 2/6  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_countcontact`num1'  {		
		gen a_countcontact`num1'`num2'_hd=.
		replace a_countcontact`num1'`num2'_hd=1 if a_countcontact`num1'==a_countcontact`num2' & a_countcontact`num1'<. & a_countcontact`num2'<.
		replace a_countcontact`num1'`num2'_hd=0 if a_countcontact`num1'!=a_countcontact`num2' & a_countcontact`num1'<. & a_countcontact`num2'<.
		lab var a_countcontact`num1'`num2'_hd "Hauptdiagonale: countcontact 2009"
		tab a_countcontact`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_countcontact`num1'  {		
		gen b_countcontact`num1'`num2'_hd=.
		replace b_countcontact`num1'`num2'_hd=1 if b_countcontact`num1'==b_countcontact`num2' & b_countcontact`num1'<. & b_countcontact`num2'<.
		replace b_countcontact`num1'`num2'_hd=0 if b_countcontact`num1'!=b_countcontact`num2' & b_countcontact`num1'<. & b_countcontact`num2'<.
		lab var b_countcontact`num1'`num2'_hd "Hauptdiagonale: countcontact 2013"
		tab b_countcontact`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}
	
	
	
	
	

	
****Aggregation on a complete main diagonal for all successive waves

foreach year in a b {
	foreach var in `year'_oeff `year'_privat `year'_paper `year'_bild `year'_paper_and `year'_magazine `year'_polinter `year'_talk { 
		gen `var'_hd_ges=(`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd+`var'67_hd)/5
	}
}


foreach year in a b {
	foreach var in `year'_countcontact {
		gen `var'_hd_ges=(`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd)/4
	}
}

****Drop single main diagonals


foreach year in a b {
	foreach var in `year'_oeff `year'_privat `year'_paper `year'_bild `year'_paper_and `year'_magazine `year'_polinter `year'_talk `year'_countcontact {
		drop `var'??_hd
	}
}



*We save the data before running the intra-individual regressions
save "${data}\data2009_2013 participants 7 waves.dta", replace


 
*---------------------------------------------------------------------
* Intraindividual Dynamic: Individual Regression over Time
*----------------------------------------------------------------------
* Creating ID-variable for loops

/*By employing a loop over all respondents, idividual regression lines should be
calculated for mode of reception over time. Since not all respondents possess valid
values for all reception variables in each wave (refuse to answer/do not know),
the loop would stop when encountering such a case. This is the reason for the
ID-variable which only contains values for those respondents where the conditions
are met. This guarantees the functioning of the loop without problems for this
new variable.
The condition is that the respondents have a valid measure on the main diagonal.
*/


foreach year in a b {
foreach var in oeff privat paper bild paper_and magazine polinter ///
	talk countcontact {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}



* Transform data into long format 

keep a_oeff* a_privat* a_paper* a_bild* a_paper_and* a_magazine* ///
	a_polinter* a_talk* a_countcontact* ///
	b_oeff* b_privat* b_paper* b_bild* b_talk* b_paper_and* b_magazine* ///
	b_polinter* b_countcontact* lfdn a_id* b_id*
reshape long a_oeff a_privat a_paper a_bild a_paper_and a_magazine ///
	a_polinter a_talk a_countcontact ///
	b_oeff b_privat b_paper b_bild b_talk b_paper_and b_magazine ///
	b_polinter b_countcontact, i(lfdn) j(welle)

	
* Individual linear regression

/*For each individual we want to know whether reception increases, decreases or 
remains stable during the campaign. This information is saved in `year'_koeff_`var'*/

foreach year in a b {
foreach var in oeff privat paper bild paper_and ///
	magazine polinter talk  {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* Fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num' & welle~=1
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num' & welle~=1
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	 & welle~=1		//calcualte p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num'  & welle~=1
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num'  & welle~=1
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num'  & welle~=1
drop p

}
}
}

foreach year in a b {
foreach var in countcontact  {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* Fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num' & welle~=1 & welle~=7
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num' & welle~=1 & welle~=7
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	 & welle~=1	& welle~=7	//calcualte p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num'  & welle~=1 & welle~=7
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num'  & welle~=1 & welle~=7
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num'  & welle~=1 & welle~=7
drop p

}
}
}

fre *koeff*	
										
//check  number of cases
sum a_id_oeff if welle~=1
sum a_koeff_oeff 	//correct!

sum b_id_magazine if welle~=1
sum b_koeff_magazine		//correct!

list a_oeff if a_koeff_oeff==-1, sepby(a_id_oeff)
fre b_koeff_oeff

* Save coefficients in new dataset since procedure is very time-consuming
reshape wide a_oeff a_privat a_paper a_bild a_paper_and a_magazine ///
	a_polinter a_talk a_countcontact ///
	b_oeff b_privat b_paper b_bild b_talk b_paper_and b_magazine ///
	b_polinter b_countcontact a_koeff_oeff a_koeff_privat ///
	a_koeff_paper a_koeff_bild a_koeff_paper_and a_koeff_magazine ///
	a_koeff_polinter a_koeff_talk a_koeff_countcontact ///
	b_koeff_oeff b_koeff_privat ///
	b_koeff_paper b_koeff_bild b_koeff_paper_and b_koeff_magazine ///
	b_koeff_polinter b_koeff_talk b_koeff_countcontact ///
	a_rohkoeff_oeff a_rohkoeff_privat ///
	a_rohkoeff_paper a_rohkoeff_bild a_rohkoeff_paper_and a_rohkoeff_magazine ///
	a_rohkoeff_polinter a_rohkoeff_talk a_rohkoeff_countcontact ///
	b_rohkoeff_oeff b_rohkoeff_privat ///
	b_rohkoeff_paper b_rohkoeff_bild b_rohkoeff_paper_and b_rohkoeff_magazine ///
	b_rohkoeff_polinter b_rohkoeff_talk b_rohkoeff_countcontact, ///
	i(lfdn) j(welle)
	
keep lfdn ?_koeff_*2 ?_rohkoeff_*2		
							//Coefficient-variable from one wave is sufficient,
							//since it's constant (after all the coefficient was 
							//calculated using all waves)

foreach ding in oeff privat paper bild paper_and magazine polinter ///
	talk countcontact {
rename ?_koeff_`ding'2 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'2 ?_rohkoeff_`ding'
}

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient"
lab val ?_koeff_* koeff

save "${data}\data 2009 2013 IIR Chapter 2.dta", replace


*-----------------------------------------------------------------
* Table: Stability / Dynamic of Receptionfrequencies
*----------------------------------------------------------------

/* Measure of stability: average main diagonal percentages crossed with
intraindividual regression coefficients.

First, merge coefficients (to data)*/

use "${data}\data2009_2013 participants 7 waves.dta",clear

//drop _merge
merge 1:1 lfdn using "${data}\data 2009 2013 IIR Chapter 2.dta"
drop _merge

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient", replace
lab val ?_koeff_* koeff


* Categorize raw coefficients (without test of significance)


foreach year in a b {
	foreach var in oeff privat paper bild paper_and magazine polinter talk countcontact {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}


* Final tables		

**************************************************
***tables 2 and 3
**************************************************		
		
***Aggregate wave means
estimates clear
foreach year in a b {
foreach var in oeff privat paper bild paper_and magazine polinter ///
	talk {

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/6
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/3
tab1 `year'_sum24`var'
gen `year'_sum57`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum57`var'

}
}


foreach year in a b {
foreach var in countcontact {

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6)/5
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/3
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'5+`year'_mean`var'6)/2
tab1 `year'_sum56`var'
}
}



*Aggregate stability preparation
*Integer values needed

foreach year in a b{
	foreach var in oeff privat paper bild paper_and magazine polinter talk {
		foreach num of numlist 2/7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*7)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

*First the differences between answers from successive waves are calculated.
*In the next step (1) deviation from stability is calculated. It is taken into account
*that, depending on  the answers given in the previou wave, respondents can only
*possess a limited deviation.

foreach year in a b{
	foreach var in oeff privat paper bild paper_and magazine polinter talk {
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/7) if `year'_`var'2n==0 | `year'_`var'2n==7
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/6) if `year'_`var'2n==1 | `year'_`var'2n==6
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/5) if `year'_`var'2n==2 | `year'_`var'2n==5
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/4) if `year'_`var'2n==3 | `year'_`var'2n==4
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/7) if `year'_`var'3n==0 | `year'_`var'3n==7
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/6) if `year'_`var'3n==1 | `year'_`var'3n==6
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/5) if `year'_`var'3n==2 | `year'_`var'3n==5
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/4) if `year'_`var'3n==3 | `year'_`var'3n==4
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/7) if `year'_`var'4n==0 | `year'_`var'4n==7
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/6) if `year'_`var'4n==1 | `year'_`var'4n==6
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/5) if `year'_`var'4n==2 | `year'_`var'4n==5
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/4) if `year'_`var'4n==3 | `year'_`var'4n==4
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/7) if `year'_`var'5n==0 | `year'_`var'5n==7
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/6) if `year'_`var'5n==1 | `year'_`var'5n==6
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/5) if `year'_`var'5n==2 | `year'_`var'5n==5
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/4) if `year'_`var'5n==3 | `year'_`var'5n==4
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==0 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==1 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==2 | `year'_`var'6n==5
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/4) if `year'_`var'6n==3 | `year'_`var'6n==4
tab `year'_stab_`var'67

}
}

*Stability for consequtive waves
foreach year in a b{
	foreach var in oeff privat paper bild paper_and magazine polinter talk {
		foreach num in 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a b{
	foreach var in oeff privat paper bild paper_and magazine polinter talk {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/5
		tab `year'_stab_`var'_aggr
}
}


*Percentage intra-individual change

*The tables contain the accumulated percentages of increasing, decreasing and stable
*answers by the respondents. The last value in column 1 indicates decreasing 
*values, the last value in column 3 indicates increasing values.
*Stable and fluctuating values are displayed in the middle column. The second-last
*value in column 2 indicates stable values. In this case, respondents display
*stable values on the main diagonal as well as no fluctuation in intra-individual
*regressions. The fluctuating values are those who show variance between their
*answers, however, the values rise as well as fall. They can be calculated by
*substracting the second-last value in column 2 from the last value in column 2.

foreach year in a b {
	foreach var in oeff privat paper bild paper_and magazine polinter talk countcontact {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}


*Drop unneccessary aiding variables

foreach year in a b{
	foreach var in oeff privat paper bild paper_and magazine polinter talk {
	drop `year'_diff_`var'??
	drop `year'_stab_`var'??
	drop `year'_stab_`var'??_mean
	drop `year'_`var'?n
}
}



**************************************************
***Table 4
**************************************************				
tab a_duell6 [aw=a_weight]
tab b_duell5 [aw=b_weight]
tab b_arena_merk6 [aw=b_weight]
tab b_arena_stein6 [aw=b_weight]
tab b_wahlomat5 [aw=b_weight]
tab b_wahlomat6 [aw=b_weight]
tab b_wahlomat7 [aw=b_weight]



save "${data}\data2009_2013 participants 7 waves.dta", replace

	